package com.cxs.shopstore.controllers;

import com.cxs.shopstore.config.ImgConf;
import com.cxs.shopstore.config.UserHeaderImgConf;
import com.cxs.shopstore.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/*
 * @Author:cxs
 * @Date:2021/5/9  8:55
 * @Version 1.0
 * */
@RestController
@SuppressWarnings("all")
public class UploadController {
    @Autowired
    private ImgConf imgConf;
    @Autowired
    private UserHeaderImgConf userHeaderImgConf;
    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 商品图片上传
     * @param file
     * @return
     */
    @PostMapping("/goodImgUpload")
    public Map<String,Object> goodImgUpload(@RequestParam("file") MultipartFile file){
        Map<String,Object> map = new HashMap<>();
        String imgPath = fileUploadService.imgupload(file, imgConf.getBasePath());
        map.put("imgPath",imgPath);
        return map;
    }

    /**
     * 用户头像上传
     * @param file
     * @return
     */
    @PostMapping("/userImgUpload")
    public Map<String,Object> userImgUpload(@RequestParam("file") MultipartFile file){
        Map<String,Object> map = new HashMap<>();
        System.out.println(userHeaderImgConf.getBasePath());
        String imgPath = fileUploadService.imgupload(file, userHeaderImgConf.getBasePath());
        map.put("imgPath",imgPath);
        return map;
    }
}
